﻿using System.IO;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.GZip;

namespace PhiStore.Experiments.Compression
{
    class GZ
    {
        public static Stream Compress(byte[] input, int bufferSize)
        {
            MemoryStream output = new MemoryStream();

            GZipOutputStream compressor = new GZipOutputStream(output);

            for(int i = 0; i < input.Length; i+= bufferSize){
                int length = (int) (i + bufferSize > input.Length ? input.Length - i : bufferSize);
              
                compressor.Write(input, i, length);
            }

            compressor.Finish();
            return output;
        }

        public static Stream Decompress(Stream input, int bufferSize)
        {
            MemoryStream output = new MemoryStream();

            GZipInputStream decompressor = new GZipInputStream(input);

            byte[] buffer = new byte[bufferSize];
            StreamUtils.Copy(decompressor, output, buffer);
        
            return output;
        }
    }
}
